<?php
class db_sql
{
    var $debug = "on";
    var $nb_requete = 0;
    var $open_link = "";
	
	public function connect($dbhost, $dbuser, $pass)
	{
		$this->open_link = @mysqli_connect($dbhost, $dbuser, $pass);
		if(!$this->open_link)
			return false;
		else
			return true;		
	}
	
	public function selectdb($db_base)
	{
		$sdb = @mysqli_select_db($this->open_link, $db_base);
        if(!$sdb)
			return false;
        else
			return true;
	}
                
    public function sql_connect($dbhost, $dbuser, $pass, $dbbasse)
    {
        
        $this->open_link = @mysqli_connect($dbhost, $dbuser, $pass);
		if(!$this->open_link  && $this->debug == "on")
        {
             $this->sql_debug('CONNECTION ERREUR');
			 return false;
        }
	        
        $sdb = @mysqli_select_db($this->open_link, $dbbasse);
        if(!$sdb && $this->debug == "on")
        {
             $this->sql_debug('SELECT BASE ERREUR');
			 return false;
        }

		return true;
    }
	
	public function sql_debug($requete, $fichier = '', $ligne = '')
	{
		 $error_sql = 'Une erreure sql est survenue pendant l\'excution du script. <br />'. mysqli_errno($this->open_link) . ": " . mysqli_error($this->open_link) .'<br /><b>Requete : </b>'.$requete .'<br />';
         $text="". strftime("%d/%m/%Y - %H:%M ",time()) ."|";
         $text.= mysqli_errno($this->open_link) . ": " . mysqli_error($this->open_link)."|";
         $text.= "Requete : ". $requete ."|Fichier : ". $fichier ."|Ligne : ". $ligne ."\r\n";
        /* ecriture ds le fichier*/
         WriteFile($text, 'log/sql.log');
		 SuccesAffiche($error_sql, 'index.php', 5 , 5);
	}
    
    public function sql_close()
    {
        @mysqli_close($this->open_link );
    }
    
    public function sql_query($requete, $fichier = '', $ligne ='')
    {
		$resultat_req=mysqli_query($this->open_link,$requete);
        if(!$resultat_req && $this->debug == "on") $this->sql_debug($requete, $fichier, $ligne);
        
        $this->sql_compte();
        return $resultat_req;
    }
    
    public function sql_nbresultat($sql)
    {
        $this -> nb_resultat = mysqli_num_rows($sql);
        return $this -> nb_resultat;
    }
    
    public function sql_fetchassoc($sql)
    {
        $this -> resultat = mysqli_fetch_assoc($sql);
        return $this -> resultat;
    }
    
    public function sql_fetcharray($sql)
    {
        $this -> resultat = mysqli_fetch_array($sql);
        return $this -> resultat;
    }
    
    public function sql_fetchrow($sql)
    {
        $row = mysqli_fetch_row($sql);
        return $row;
    }
    
    public function sql_fetchobject($sql)
    {
        $row = mysqli_fetch_object($sql);
        return $row;
    }
    
    public function sql_freeresult($sql)
    {
        $this-> free =  mysqli_free_result($sql);
        return true;
    }
    
    public function sql_affectedrows()
    {
        $this -> affected = mysqli_affected_rows($this->open_link);
        return $this -> affected;
    }
    
      public function sql_lastinsertid()
    {
		$lastid = mysqli_insert_id($this->open_link);
		return $lastid;
	}
    
    private function sql_compte()
    {
        $this->nb_requete ++;
    }
}

?>